﻿angular.module('app')
    .service('DashboardSrv', ['$http','helperSrv','globalConfig',
        function ($http, helperSrv, globalConfig) {
            return {
                //GET api/dashboardworkflows?woid=&wcode=&isActive=&start=&end=&pageIndex=0&pageSize=10
                getWorkflows: function (filter,pageIndex) {
                    var woid = (filter.woid !== '' && /\w{8}\-\w{4}\-\w{4}\-\w{4}\-\w{12}/g.test(filter.woid)) ? filter.woid : '';
                    //var start = (filter.start !== '' && /\d{1,2}\/\d{1,2}\/\d{4}/g.test(filter.start)) ? filter.start : '';
                    //var end = (filter.end !== '' && /\d{1,2}\/\d{1,2}\/\d{4}/g.test(filter.end)) ? filter.end : '';
                    var start = filter.start;
                    var end = filter.end;
                    if (end !== '') {
                        //Start Increase 1 day in end date
                        //date string to date object
                        var endDateObject = helperSrv.convertFormatedStringToDateObject(end);
                        //Add a day
                        endDateObject = moment(endDateObject).add('d', 1);
                        //date object to date string
                        end = helperSrv.convertDateObjectToServerDate(endDateObject);
                        //End Increase 1 day in end date
                    }
                    if (start !== '') {
                        //convert formated date string to date object
                        var startDateString = helperSrv.convertFormatedStringToDateObject(start);
                        //convert date object to server date
                        start = helperSrv.convertDateObjectToServerDate(startDateString);
                    }
                    return $http({ method: 'GET', url: 'api/dashboardworkflows', params: { woid: woid, wcode: filter.wcode, isActive: filter.isActive, start: start, end: end, pageIndex: pageIndex, pageSize: filter.pageSize }, cache: false });
                },
                //PATCH api/dashboardworkflows?op=Delete
                doWorkflow: function (action, wfids) {
                    return $http({ method: 'PATCH', url: 'api/dashboardworkflows', params: { op: action }, data: wfids, cache: false });
                },
                //GET api/dashboarddetails/7d3a5953-0055-463b-81f7-cefa81809555/
                getWorkflowDetail: function (woid) {
                    return $http({ method: 'GET', url: 'api/dashboarddetails/' + woid, cache: false });
                },
            };
        }
    ]);